package net.etfbl.projekat.verko.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Time;
import java.util.ArrayList;
import java.util.Date;

import net.etfbl.projekat.verko.dto.Film;
import net.etfbl.projekat.verko.dto.Glumac;
import net.etfbl.projekat.verko.dto.Reziser;
import net.etfbl.projekat.verko.dto.Scenarista;
import net.etfbl.projekat.verko.dto.Zanr;

//@SuppressWarnings(value = { "all" })        //treba iskomentarisati na kraju
public class FilmDAO {
	
	public static ArrayList<Film> getAllFilm(DataManager dm, Film movie)
	{
		int id_film;
		String naziv;
		double ocjena;
		Date datum_dodavanja;
		boolean aktivan;
		String sadrzaj;
		String slika_adresa;
		Time trajanje;
		String link_amazon;
		String link_imdb;
		
		ArrayList<Film> rezultat= new ArrayList<Film>();
		final String upit="select * from tbl_film";
		
		Connection conn = dm.checkOutConnection();

		if(conn!= null)
		{
			try
			{
				PreparedStatement ps= conn.prepareStatement(upit);
				try
				{
					ResultSet rs=ps.executeQuery();
					try
					{
						while(rs.next())
						{
							id_film=rs.getInt(1);
							naziv=rs.getString(2);
							ocjena=rs.getDouble(3);
							datum_dodavanja=rs.getDate(4);
							aktivan=rs.getBoolean(5);
							sadrzaj=rs.getString(6);
							slika_adresa=rs.getString(7);
							trajanje=rs.getTime(7);
							link_amazon=rs.getString(8);
							link_imdb=rs.getString(9);
							rezultat.add(new Film(id_film,naziv,ocjena,datum_dodavanja,aktivan,sadrzaj,slika_adresa,trajanje,link_amazon,link_imdb));
						}
					}
					finally
					{
						rs.close();
					}
				}
				finally
				{
					ps.close();
				}
			}
			catch(SQLException ex)
			{
				System.out.println("Greska u FilmDAO: "+ex.getMessage());
			}
			finally
			{
				dm.checkInConnection(conn);
			}
		}
		return rezultat;
	}
	
	public static ArrayList<Film> filterFilmByIme(DataManager dm, Film movie)
	{
		int id_film;
		String naziv;
		double ocjena;
		Date datum_dodavanja;
		boolean aktivan;
		String sadrzaj;
		String slika_adresa;
		Time trajanje;
		String link_amazon;
		String link_imdb;
		
		ArrayList<Film> rezultat= new ArrayList<Film>();
		final String upit="select * from tbl_film where naziv="+movie.getNaziv();
		
		Connection conn = dm.checkOutConnection();

		if(conn!= null)
		{
			try
			{
				PreparedStatement ps= conn.prepareStatement(upit);
				try
				{
					ResultSet rs=ps.executeQuery();
					try
					{
						while(rs.next())
						{
							if(rs.getBoolean(5))
								continue;
							id_film=rs.getInt(1);
							naziv=rs.getString(2);
							ocjena=rs.getDouble(3);
							datum_dodavanja=rs.getDate(4);
							aktivan=rs.getBoolean(5);
							sadrzaj=rs.getString(6);
							slika_adresa=rs.getString(7);
							trajanje=rs.getTime(7);
							link_amazon=rs.getString(8);
							link_imdb=rs.getString(9);
							rezultat.add(new Film(id_film,naziv,ocjena,datum_dodavanja,aktivan,sadrzaj,slika_adresa,trajanje,link_amazon,link_imdb));
						}
					}
					finally
					{
						rs.close();
					}
				}
				finally
				{
					ps.close();
				}
			}
			catch(SQLException ex)
			{
				System.out.println("Greska u FilmDAO: "+ex.getMessage());
			}
			finally
			{
				dm.checkInConnection(conn);
			}
		}
		return rezultat;
	}
	
	public static ArrayList<Film> filterFilmByIme(DataManager dm, String naziv1)
	{
		int id_film;
		String naziv;
		double ocjena;
		Date datum_dodavanja;
		boolean aktivan;
		String sadrzaj;
		String slika_adresa;
		Time trajanje;
		String link_amazon;
		String link_imdb;
		
		ArrayList<Film> rezultat= new ArrayList<Film>();
		final String upit="select * from tbl_film where naziv="+naziv1;
		
		Connection conn = dm.checkOutConnection();

		if(conn!= null)
		{
			try
			{
				PreparedStatement ps= conn.prepareStatement(upit);
				try
				{
					ResultSet rs=ps.executeQuery();
					try
					{
						while(rs.next())
						{
							if(rs.getBoolean(5))
								continue;
							id_film=rs.getInt(1);
							naziv=rs.getString(2);
							ocjena=rs.getDouble(3);
							datum_dodavanja=rs.getDate(4);
							aktivan=rs.getBoolean(5);
							sadrzaj=rs.getString(6);
							slika_adresa=rs.getString(7);
							trajanje=rs.getTime(7);
							link_amazon=rs.getString(8);
							link_imdb=rs.getString(9);
							rezultat.add(new Film(id_film,naziv,ocjena,datum_dodavanja,aktivan,sadrzaj,slika_adresa,trajanje,link_amazon,link_imdb));
						}
					}
					finally
					{
						rs.close();
					}
				}
				finally
				{
					ps.close();
				}
			}
			catch(SQLException ex)
			{
				System.out.println("Greska u FilmDAO: "+ex.getMessage());
			}
			finally
			{
				dm.checkInConnection(conn);
			}
		}
		return rezultat;
	}
	
	public static ArrayList<Film> filterFilmById(DataManager dm, Film movie)
	{
		int id_film;
		String naziv;
		double ocjena;
		Date datum_dodavanja;
		boolean aktivan;
		String sadrzaj;
		String slika_adresa;
		Time trajanje;
		String link_amazon;
		String link_imdb;
		
		ArrayList<Film> rezultat= new ArrayList<Film>();
		final String upit="select * from tbl_film where id_film="+movie.getId_film();
		
		Connection conn = dm.checkOutConnection();

		if(conn!= null)
		{
			try
			{
				PreparedStatement ps= conn.prepareStatement(upit);
				try
				{
					ResultSet rs=ps.executeQuery();
					try
					{
						while(rs.next())
						{
							if(rs.getBoolean(5))
								continue;
							id_film=rs.getInt(1);
							naziv=rs.getString(2);
							ocjena=rs.getDouble(3);
							datum_dodavanja=rs.getDate(4);
							aktivan=rs.getBoolean(5);
							sadrzaj=rs.getString(6);
							slika_adresa=rs.getString(7);
							trajanje=rs.getTime(7);
							link_amazon=rs.getString(8);
							link_imdb=rs.getString(9);
							rezultat.add(new Film(id_film,naziv,ocjena,datum_dodavanja,aktivan,sadrzaj,slika_adresa,trajanje,link_amazon,link_imdb));
						}
					}
					finally
					{
						rs.close();
					}
				}
				finally
				{
					ps.close();
				}
			}
			catch(SQLException ex)
			{
				System.out.println("Greska u FilmDAO: "+ex.getMessage());
			}
			finally
			{
				dm.checkInConnection(conn);
			}
		}
		return rezultat;
	}
	
	public static ArrayList<Film> filterFilmByZanr(DataManager dm, Zanr zanr)
	{
		int id_film;
		String naziv;
		double ocjena;
		Date datum_dodavanja;
		boolean aktivan;
		String sadrzaj;
		String slika_adresa;
		Time trajanje;
		String link_amazon;
		String link_imdb;
		
		ArrayList<Integer> kljucevi;
		ArrayList<Film> rezultat=new ArrayList<Film>();
		
		Connection conn= dm.checkOutConnection();
		
		final String upit1="select tbl_film_id_film from tbl_film_has_zanr where tbl_zanr_id_zanr = "+zanr.getId_zanr();
		String upit2;
		if(conn!=null)
		{
			try
			{
				PreparedStatement ps1= conn.prepareStatement(upit1);
				try
				{
					ResultSet rs1=ps1.executeQuery();
					try
					{
						kljucevi=new ArrayList<Integer>();
						while(rs1.next())
						{
							kljucevi.add(Integer.valueOf(rs1.getInt(1)));
						}
					}
					finally
					{
						rs1.close();
					}
				}
				finally
				{
					ps1.close();
				}
				
				if(kljucevi!=null)
				{
					for(Integer temp : kljucevi)
					{	
						upit2="select * from tbl_film where id_film = "+temp.intValue();
						PreparedStatement ps2=conn.prepareStatement(upit2);
							try
							{
								ResultSet rs2=ps2.executeQuery();
								try
								{
									while(rs2.next())
									{
										if(rs2.getBoolean(5))
											continue;
										id_film=rs2.getInt(1);
										naziv=rs2.getString(2);
										ocjena=rs2.getDouble(3);
										datum_dodavanja=rs2.getDate(4);
										aktivan=rs2.getBoolean(5);
										sadrzaj=rs2.getString(6);
										slika_adresa=rs2.getString(7);
										trajanje=rs2.getTime(7);
										link_amazon=rs2.getString(8);
										link_imdb=rs2.getString(9);
										rezultat.add(new Film(id_film,naziv,ocjena,datum_dodavanja,aktivan,sadrzaj,slika_adresa,trajanje,link_amazon,link_imdb));
									}
								}
								finally
								{
									rs2.close();
								}
							}
							finally
							{
								ps2.close();
							}
					}
				}
			}
			catch(SQLException ex)
			{
				System.out.println("Greska u FilmDAO: "+ex.getMessage());
			}
			finally
			{
				dm.checkInConnection(conn);
			}
		}
		
		return rezultat;
	}
	
	public static ArrayList<Film> filterFilmByGlumac(DataManager dm, Glumac glumac)
	{
		int id_film;
		String naziv;
		double ocjena;
		Date datum_dodavanja;
		boolean aktivan;
		String sadrzaj;
		String slika_adresa;
		Time trajanje;
		String link_amazon;
		String link_imdb;
		
		ArrayList<Integer> kljucevi;
		ArrayList<Film> rezultat=new ArrayList<Film>();;
		
		Connection conn= dm.checkOutConnection();
		
		final String upit1="select tbl_film_id_film from tbl_film_has_glumac where tbl_glumac_id_glumac = "+glumac.getId_glumac();
		String upit2;
		if(conn!=null)
		{
			try
			{
				PreparedStatement ps1= conn.prepareStatement(upit1);
				try
				{
					ResultSet rs1=ps1.executeQuery();
					try
					{
						kljucevi=new ArrayList<Integer>();
						while(rs1.next())
						{
							kljucevi.add(Integer.valueOf(rs1.getInt(1)));
						}
					}
					finally
					{
						rs1.close();
					}
				}
				finally
				{
					ps1.close();
				}
				
				if(kljucevi!=null)
				{
					for(Integer temp : kljucevi)
					{	
						upit2="select * from tbl_film where id_film = "+temp.intValue();
						PreparedStatement ps2=conn.prepareStatement(upit2);
							try
							{
								ResultSet rs2=ps2.executeQuery();
								try
								{
									while(rs2.next())
									{
										if(rs2.getBoolean(5))
											continue;
										id_film=rs2.getInt(1);
										naziv=rs2.getString(2);
										ocjena=rs2.getDouble(3);
										datum_dodavanja=rs2.getDate(4);
										aktivan=rs2.getBoolean(5);
										sadrzaj=rs2.getString(6);
										slika_adresa=rs2.getString(7);
										trajanje=rs2.getTime(7);
										link_amazon=rs2.getString(8);
										link_imdb=rs2.getString(9);
										rezultat.add(new Film(id_film,naziv,ocjena,datum_dodavanja,aktivan,sadrzaj,slika_adresa,trajanje,link_amazon,link_imdb));
									}
								}
								finally
								{
									rs2.close();
								}
							}
							finally
							{
								ps2.close();
							}
					}
				}
			}
			catch(SQLException ex)
			{
				System.out.println("Greska u FilmDAO: "+ex.getMessage());
			}
			finally
			{
				dm.checkInConnection(conn);
			}
		}
		
		return rezultat;
	}
	
	public static ArrayList<Film> filterFilmByReziser(DataManager dm, Reziser reziser)
	{
		int id_film;
		String naziv;
		double ocjena;
		Date datum_dodavanja;
		boolean aktivan;
		String sadrzaj;
		String slika_adresa;
		Time trajanje;
		String link_amazon;
		String link_imdb;
		
		ArrayList<Integer> kljucevi;
		ArrayList<Film> rezultat=new ArrayList<Film>();;
		
		Connection conn= dm.checkOutConnection();
		
		final String upit1="select tbl_film_id_film from tbl_film_has_reziser where tbl_reziser_id_reziser = "+reziser.getId_reziser();
		String upit2;
		if(conn!=null)
		{
			try
			{
				PreparedStatement ps1= conn.prepareStatement(upit1);
				try
				{
					ResultSet rs1=ps1.executeQuery();
					try
					{
						kljucevi=new ArrayList<Integer>();
						while(rs1.next())
						{
							kljucevi.add(Integer.valueOf(rs1.getInt(1)));
						}
					}
					finally
					{
						rs1.close();
					}
				}
				finally
				{
					ps1.close();
				}
				
				if(kljucevi!=null)
				{
					for(Integer temp : kljucevi)
					{	
						upit2="select * from tbl_film where id_film = "+temp.intValue();
						PreparedStatement ps2=conn.prepareStatement(upit2);
							try
							{
								ResultSet rs2=ps2.executeQuery();
								try
								{
									while(rs2.next())
									{
										if(rs2.getBoolean(5))
											continue;
										id_film=rs2.getInt(1);
										naziv=rs2.getString(2);
										ocjena=rs2.getDouble(3);
										datum_dodavanja=rs2.getDate(4);
										aktivan=rs2.getBoolean(5);
										sadrzaj=rs2.getString(6);
										slika_adresa=rs2.getString(7);
										trajanje=rs2.getTime(7);
										link_amazon=rs2.getString(8);
										link_imdb=rs2.getString(9);
										rezultat.add(new Film(id_film,naziv,ocjena,datum_dodavanja,aktivan,sadrzaj,slika_adresa,trajanje,link_amazon,link_imdb));
									}
								}
								finally
								{
									rs2.close();
								}
							}
							finally
							{
								ps2.close();
							}
					}
				}
			}
			catch(SQLException ex)
			{
				System.out.println("Greska u FilmDAO: "+ex.getMessage());
			}
			finally
			{
				dm.checkInConnection(conn);
			}
		}
		
		return rezultat;
	}
	
	public static ArrayList<Film> filterFilmByScenarista(DataManager dm, Scenarista scenarista)
	{
		int id_film;
		String naziv;
		double ocjena;
		Date datum_dodavanja;
		boolean aktivan;
		String sadrzaj;
		String slika_adresa;
		Time trajanje;
		String link_amazon;
		String link_imdb;
		
		ArrayList<Integer> kljucevi;
		ArrayList<Film> rezultat=new ArrayList<Film>();;
		
		Connection conn= dm.checkOutConnection();
		
		final String upit1="select tbl_film_id_film from tbl_film_has_scenarista where tbl_scenarista_id_scenarista = "+scenarista.getId_scenarista();
		String upit2;
		if(conn!=null)
		{
			try
			{
				PreparedStatement ps1= conn.prepareStatement(upit1);
				try
				{
					ResultSet rs1=ps1.executeQuery();
					try
					{
						kljucevi=new ArrayList<Integer>();
						while(rs1.next())
						{
							kljucevi.add(Integer.valueOf(rs1.getInt(1)));
						}
					}
					finally
					{
						rs1.close();
					}
				}
				finally
				{
					ps1.close();
				}
				
				if(kljucevi!=null)
				{
					for(Integer temp : kljucevi)
					{	
						upit2="select * from tbl_film where id_film = "+temp.intValue();
						PreparedStatement ps2=conn.prepareStatement(upit2);
							try
							{
								ResultSet rs2=ps2.executeQuery();
								try
								{
									while(rs2.next())
									{
										if(rs2.getBoolean(5))
											continue;
										id_film=rs2.getInt(1);
										naziv=rs2.getString(2);
										ocjena=rs2.getDouble(3);
										datum_dodavanja=rs2.getDate(4);
										aktivan=rs2.getBoolean(5);
										sadrzaj=rs2.getString(6);
										slika_adresa=rs2.getString(7);
										trajanje=rs2.getTime(7);
										link_amazon=rs2.getString(8);
										link_imdb=rs2.getString(9);
										rezultat.add(new Film(id_film,naziv,ocjena,datum_dodavanja,aktivan,sadrzaj,slika_adresa,trajanje,link_amazon,link_imdb));
									}
								}
								finally
								{
									rs2.close();
								}
							}
							finally
							{
								ps2.close();
							}
					}
				}
			}
			catch(SQLException ex)
			{
				System.out.println("Greska u FilmDAO: "+ex.getMessage());
			}
			finally
			{
				dm.checkInConnection(conn);
			}
		}
		
		return rezultat;
	}
	
	public static ArrayList<Film> filterFilmByOcjena(DataManager dm, Film movie)
	{
		int id_film;
		String naziv;
		double ocjena;
		Date datum_dodavanja;
		boolean aktivan;
		String sadrzaj;
		String slika_adresa;
		Time trajanje;
		String link_amazon;
		String link_imdb;
		
		ArrayList<Film> rezultat= new ArrayList<Film>();
		final String upit="select * from tbl_film where ocjena > "+Math.floor(movie.getOcjena())+" and ocjena < "+Math.ceil(movie.getOcjena());
		
		Connection conn = dm.checkOutConnection();

		if(conn!= null)
		{
			try
			{
				PreparedStatement ps= conn.prepareStatement(upit);
				try
				{
					ResultSet rs=ps.executeQuery();
					try
					{
						while(rs.next())
						{
							if(rs.getBoolean(5))
								continue;
							id_film=rs.getInt(1);
							naziv=rs.getString(2);
							ocjena=rs.getDouble(3);
							datum_dodavanja=rs.getDate(4);
							aktivan=rs.getBoolean(5);
							sadrzaj=rs.getString(6);
							slika_adresa=rs.getString(7);
							trajanje=rs.getTime(7);
							link_amazon=rs.getString(8);
							link_imdb=rs.getString(9);
							rezultat.add(new Film(id_film,naziv,ocjena,datum_dodavanja,aktivan,sadrzaj,slika_adresa,trajanje,link_amazon,link_imdb));
						}
					}
					finally
					{
						rs.close();
					}
				}
				finally
				{
					ps.close();
				}
			}
			catch(SQLException ex)
			{
				System.out.println("Greska u FilmDAO: "+ex.getMessage());
			}
			finally
			{
				dm.checkInConnection(conn);
			}
		}
		return rezultat;
	}
	
	public static ArrayList<Film> filterFilmByOcjena(DataManager dm, double ocjena1)
	{
		int id_film;
		String naziv;
		double ocjena;
		Date datum_dodavanja;
		boolean aktivan;
		String sadrzaj;
		String slika_adresa;
		Time trajanje;
		String link_amazon;
		String link_imdb;
		
		ArrayList<Film> rezultat= new ArrayList<Film>();
		final String upit="select * from tbl_film where ocjena > "+Math.floor(ocjena1)+" and ocjena < "+Math.ceil(ocjena1);
		
		Connection conn = dm.checkOutConnection();

		if(conn!= null)
		{
			try
			{
				PreparedStatement ps= conn.prepareStatement(upit);
				try
				{
					ResultSet rs=ps.executeQuery();
					try
					{
						while(rs.next())
						{
							if(rs.getBoolean(5))
								continue;
							id_film=rs.getInt(1);
							naziv=rs.getString(2);
							ocjena=rs.getDouble(3);
							datum_dodavanja=rs.getDate(4);
							aktivan=rs.getBoolean(5);
							sadrzaj=rs.getString(6);
							slika_adresa=rs.getString(7);
							trajanje=rs.getTime(7);
							link_amazon=rs.getString(8);
							link_imdb=rs.getString(9);
							rezultat.add(new Film(id_film,naziv,ocjena,datum_dodavanja,aktivan,sadrzaj,slika_adresa,trajanje,link_amazon,link_imdb));
						}
					}
					finally
					{
						rs.close();
					}
				}
				finally
				{
					ps.close();
				}
			}
			catch(SQLException ex)
			{
				System.out.println("Greska u FilmDAO: "+ex.getMessage());
			}
			finally
			{
				dm.checkInConnection(conn);
			}
		}
		return rezultat;
	}
	
	public static ArrayList<Film> filterFilmByImeAdmin(DataManager dm, Film movie)
	{
		int id_film;
		String naziv;
		double ocjena;
		Date datum_dodavanja;
		boolean aktivan;
		String sadrzaj;
		String slika_adresa;
		Time trajanje;
		String link_amazon;
		String link_imdb;
		
		ArrayList<Film> rezultat= new ArrayList<Film>();
		final String upit="select * from tbl_film where naziv="+movie.getNaziv();
		
		Connection conn = dm.checkOutConnection();

		if(conn!= null)
		{
			try
			{
				PreparedStatement ps= conn.prepareStatement(upit);
				try
				{
					ResultSet rs=ps.executeQuery();
					try
					{
						while(rs.next())
						{
							id_film=rs.getInt(1);
							naziv=rs.getString(2);
							ocjena=rs.getDouble(3);
							datum_dodavanja=rs.getDate(4);
							aktivan=rs.getBoolean(5);
							sadrzaj=rs.getString(6);
							slika_adresa=rs.getString(7);
							trajanje=rs.getTime(7);
							link_amazon=rs.getString(8);
							link_imdb=rs.getString(9);
							rezultat.add(new Film(id_film,naziv,ocjena,datum_dodavanja,aktivan,sadrzaj,slika_adresa,trajanje,link_amazon,link_imdb));
						}
					}
					finally
					{
						rs.close();
					}
				}
				finally
				{
					ps.close();
				}
			}
			catch(SQLException ex)
			{
				System.out.println("Greska u FilmDAO: "+ex.getMessage());
			}
			finally
			{
				dm.checkInConnection(conn);
			}
		}
		return rezultat;
	}
	
	public static ArrayList<Film> filterFilmByImeAdmin(DataManager dm, String naziv1)
	{
		int id_film;
		String naziv;
		double ocjena;
		Date datum_dodavanja;
		boolean aktivan;
		String sadrzaj;
		String slika_adresa;
		Time trajanje;
		String link_amazon;
		String link_imdb;
		
		ArrayList<Film> rezultat= new ArrayList<Film>();
		final String upit="select * from tbl_film where naziv="+naziv1;
		
		Connection conn = dm.checkOutConnection();

		if(conn!= null)
		{
			try
			{
				PreparedStatement ps= conn.prepareStatement(upit);
				try
				{
					ResultSet rs=ps.executeQuery();
					try
					{
						while(rs.next())
						{
							id_film=rs.getInt(1);
							naziv=rs.getString(2);
							ocjena=rs.getDouble(3);
							datum_dodavanja=rs.getDate(4);
							aktivan=rs.getBoolean(5);
							sadrzaj=rs.getString(6);
							slika_adresa=rs.getString(7);
							trajanje=rs.getTime(7);
							link_amazon=rs.getString(8);
							link_imdb=rs.getString(9);
							rezultat.add(new Film(id_film,naziv,ocjena,datum_dodavanja,aktivan,sadrzaj,slika_adresa,trajanje,link_amazon,link_imdb));
						}
					}
					finally
					{
						rs.close();
					}
				}
				finally
				{
					ps.close();
				}
			}
			catch(SQLException ex)
			{
				System.out.println("Greska u FilmDAO: "+ex.getMessage());
			}
			finally
			{
				dm.checkInConnection(conn);
			}
		}
		return rezultat;
	}
	
	public static ArrayList<Film> filterFilmByIdAdmin(DataManager dm, Film movie)
	{
		int id_film;
		String naziv;
		double ocjena;
		Date datum_dodavanja;
		boolean aktivan;
		String sadrzaj;
		String slika_adresa;
		Time trajanje;
		String link_amazon;
		String link_imdb;
		
		ArrayList<Film> rezultat= new ArrayList<Film>();
		final String upit="select * from tbl_film where id_film="+movie.getId_film();
		
		Connection conn = dm.checkOutConnection();

		if(conn!= null)
		{
			try
			{
				PreparedStatement ps= conn.prepareStatement(upit);
				try
				{
					ResultSet rs=ps.executeQuery();
					try
					{
						while(rs.next())
						{
							id_film=rs.getInt(1);
							naziv=rs.getString(2);
							ocjena=rs.getDouble(3);
							datum_dodavanja=rs.getDate(4);
							aktivan=rs.getBoolean(5);
							sadrzaj=rs.getString(6);
							slika_adresa=rs.getString(7);
							trajanje=rs.getTime(7);
							link_amazon=rs.getString(8);
							link_imdb=rs.getString(9);
							rezultat.add(new Film(id_film,naziv,ocjena,datum_dodavanja,aktivan,sadrzaj,slika_adresa,trajanje,link_amazon,link_imdb));
						}
					}
					finally
					{
						rs.close();
					}
				}
				finally
				{
					ps.close();
				}
			}
			catch(SQLException ex)
			{
				System.out.println("Greska u FilmDAO: "+ex.getMessage());
			}
			finally
			{
				dm.checkInConnection(conn);
			}
		}
		return rezultat;
	}
	
	public static ArrayList<Film> filterFilmByZanrAdmin(DataManager dm, Zanr zanr)
	{
		int id_film;
		String naziv;
		double ocjena;
		Date datum_dodavanja;
		boolean aktivan;
		String sadrzaj;
		String slika_adresa;
		Time trajanje;
		String link_amazon;
		String link_imdb;
		
		ArrayList<Integer> kljucevi;
		ArrayList<Film> rezultat=new ArrayList<Film>();;
		
		Connection conn= dm.checkOutConnection();
		
		final String upit1="select tbl_film_id_film from tbl_film_has_zanr where tbl_zanr_id_zanr = "+zanr.getId_zanr();
		String upit2;
		if(conn!=null)
		{
			try
			{
				PreparedStatement ps1= conn.prepareStatement(upit1);
				try
				{
					ResultSet rs1=ps1.executeQuery();
					try
					{
						kljucevi=new ArrayList<Integer>();
						while(rs1.next())
						{
							kljucevi.add(Integer.valueOf(rs1.getInt(1)));
						}
					}
					finally
					{
						rs1.close();
					}
				}
				finally
				{
					ps1.close();
				}
				
				if(kljucevi!=null)
				{
					for(Integer temp : kljucevi)
					{	
						upit2="select * from tbl_film where id_film = "+temp.intValue();
						PreparedStatement ps2=conn.prepareStatement(upit2);
							try
							{
								ResultSet rs2=ps2.executeQuery();
								try
								{
									while(rs2.next())
									{
										id_film=rs2.getInt(1);
										naziv=rs2.getString(2);
										ocjena=rs2.getDouble(3);
										datum_dodavanja=rs2.getDate(4);
										aktivan=rs2.getBoolean(5);
										sadrzaj=rs2.getString(6);
										slika_adresa=rs2.getString(7);
										trajanje=rs2.getTime(7);
										link_amazon=rs2.getString(8);
										link_imdb=rs2.getString(9);
										rezultat.add(new Film(id_film,naziv,ocjena,datum_dodavanja,aktivan,sadrzaj,slika_adresa,trajanje,link_amazon,link_imdb));
									}
								}
								finally
								{
									rs2.close();
								}
							}
							finally
							{
								ps2.close();
							}
					}
				}
			}
			catch(SQLException ex)
			{
				System.out.println("Greska u FilmDAO: "+ex.getMessage());
			}
			finally
			{
				dm.checkInConnection(conn);
			}
		}
		
		return rezultat;
	}
	
	public static ArrayList<Film> filterFilmByGlumacAdmin(DataManager dm, Glumac glumac)
	{
		int id_film;
		String naziv;
		double ocjena;
		Date datum_dodavanja;
		boolean aktivan;
		String sadrzaj;
		String slika_adresa;
		Time trajanje;
		String link_amazon;
		String link_imdb;
		
		ArrayList<Integer> kljucevi;
		ArrayList<Film> rezultat=new ArrayList<Film>();;
		
		Connection conn= dm.checkOutConnection();
		
		final String upit1="select tbl_film_id_film from tbl_film_has_glumac where tbl_glumac_id_glumac = "+glumac.getId_glumac();
		String upit2;
		if(conn!=null)
		{
			try
			{
				PreparedStatement ps1= conn.prepareStatement(upit1);
				try
				{
					ResultSet rs1=ps1.executeQuery();
					try
					{
						kljucevi=new ArrayList<Integer>();
						while(rs1.next())
						{
							kljucevi.add(Integer.valueOf(rs1.getInt(1)));
						}
					}
					finally
					{
						rs1.close();
					}
				}
				finally
				{
					ps1.close();
				}
				
				if(kljucevi!=null)
				{
					for(Integer temp : kljucevi)
					{	
						upit2="select * from tbl_film where id_film = "+temp.intValue();
						PreparedStatement ps2=conn.prepareStatement(upit2);
							try
							{
								ResultSet rs2=ps2.executeQuery();
								try
								{
									while(rs2.next())
									{
										id_film=rs2.getInt(1);
										naziv=rs2.getString(2);
										ocjena=rs2.getDouble(3);
										datum_dodavanja=rs2.getDate(4);
										aktivan=rs2.getBoolean(5);
										sadrzaj=rs2.getString(6);
										slika_adresa=rs2.getString(7);
										trajanje=rs2.getTime(7);
										link_amazon=rs2.getString(8);
										link_imdb=rs2.getString(9);
										rezultat.add(new Film(id_film,naziv,ocjena,datum_dodavanja,aktivan,sadrzaj,slika_adresa,trajanje,link_amazon,link_imdb));
									}
								}
								finally
								{
									rs2.close();
								}
							}
							finally
							{
								ps2.close();
							}
					}
				}
			}
			catch(SQLException ex)
			{
				System.out.println("Greska u FilmDAO: "+ex.getMessage());
			}
			finally
			{
				dm.checkInConnection(conn);
			}
		}
		
		return rezultat;
	}
	
	public static ArrayList<Film> filterFilmByReziserAdmin(DataManager dm, Reziser reziser)
	{
		int id_film;
		String naziv;
		double ocjena;
		Date datum_dodavanja;
		boolean aktivan;
		String sadrzaj;
		String slika_adresa;
		Time trajanje;
		String link_amazon;
		String link_imdb;
		
		ArrayList<Integer> kljucevi;
		ArrayList<Film> rezultat=new ArrayList<Film>();;
		
		Connection conn= dm.checkOutConnection();
		
		final String upit1="select tbl_film_id_film from tbl_film_has_reziser where tbl_reziser_id_reziser = "+reziser.getId_reziser();
		String upit2;
		if(conn!=null)
		{
			try
			{
				PreparedStatement ps1= conn.prepareStatement(upit1);
				try
				{
					ResultSet rs1=ps1.executeQuery();
					try
					{
						kljucevi=new ArrayList<Integer>();
						while(rs1.next())
						{
							kljucevi.add(Integer.valueOf(rs1.getInt(1)));
						}
					}
					finally
					{
						rs1.close();
					}
				}
				finally
				{
					ps1.close();
				}
				
				if(kljucevi!=null)
				{
					for(Integer temp : kljucevi)
					{	
						upit2="select * from tbl_film where id_film = "+temp.intValue();
						PreparedStatement ps2=conn.prepareStatement(upit2);
							try
							{
								ResultSet rs2=ps2.executeQuery();
								try
								{
									while(rs2.next())
									{
										id_film=rs2.getInt(1);
										naziv=rs2.getString(2);
										ocjena=rs2.getDouble(3);
										datum_dodavanja=rs2.getDate(4);
										aktivan=rs2.getBoolean(5);
										sadrzaj=rs2.getString(6);
										slika_adresa=rs2.getString(7);
										trajanje=rs2.getTime(7);
										link_amazon=rs2.getString(8);
										link_imdb=rs2.getString(9);
										rezultat.add(new Film(id_film,naziv,ocjena,datum_dodavanja,aktivan,sadrzaj,slika_adresa,trajanje,link_amazon,link_imdb));
									}
								}
								finally
								{
									rs2.close();
								}
							}
							finally
							{
								ps2.close();
							}
					}
				}
			}
			catch(SQLException ex)
			{
				System.out.println("Greska u FilmDAO: "+ex.getMessage());
			}
			finally
			{
				dm.checkInConnection(conn);
			}
		}
		
		return rezultat;
	}
	
	public static ArrayList<Film> filterFilmByScenaristaAdmin(DataManager dm, Scenarista scenarista)
	{
		int id_film;
		String naziv;
		double ocjena;
		Date datum_dodavanja;
		boolean aktivan;
		String sadrzaj;
		String slika_adresa;
		Time trajanje;
		String link_amazon;
		String link_imdb;
		
		ArrayList<Integer> kljucevi;
		ArrayList<Film> rezultat=new ArrayList<Film>();;
		
		Connection conn= dm.checkOutConnection();
		
		final String upit1="select tbl_film_id_film from tbl_film_has_scenarista where tbl_scenarista_id_scenarista = "+scenarista.getId_scenarista();
		String upit2;
		if(conn!=null)
		{
			try
			{
				PreparedStatement ps1= conn.prepareStatement(upit1);
				try
				{
					ResultSet rs1=ps1.executeQuery();
					try
					{
						kljucevi=new ArrayList<Integer>();
						while(rs1.next())
						{
							kljucevi.add(Integer.valueOf(rs1.getInt(1)));
						}
					}
					finally
					{
						rs1.close();
					}
				}
				finally
				{
					ps1.close();
				}
				
				if(kljucevi!=null)
				{
					for(Integer temp : kljucevi)
					{	
						upit2="select * from tbl_film where id_film = "+temp.intValue();
						PreparedStatement ps2=conn.prepareStatement(upit2);
							try
							{
								ResultSet rs2=ps2.executeQuery();
								try
								{
									while(rs2.next())
									{
										id_film=rs2.getInt(1);
										naziv=rs2.getString(2);
										ocjena=rs2.getDouble(3);
										datum_dodavanja=rs2.getDate(4);
										aktivan=rs2.getBoolean(5);
										sadrzaj=rs2.getString(6);
										slika_adresa=rs2.getString(7);
										trajanje=rs2.getTime(7);
										link_amazon=rs2.getString(8);
										link_imdb=rs2.getString(9);
										rezultat.add(new Film(id_film,naziv,ocjena,datum_dodavanja,aktivan,sadrzaj,slika_adresa,trajanje,link_amazon,link_imdb));
									}
								}
								finally
								{
									rs2.close();
								}
							}
							finally
							{
								ps2.close();
							}
					}
				}
			}
			catch(SQLException ex)
			{
				System.out.println("Greska u FilmDAO: "+ex.getMessage());
			}
			finally
			{
				dm.checkInConnection(conn);
			}
		}
		
		return rezultat;
	}
	
	public static ArrayList<Film> filterFilmByOcjenaAdmin(DataManager dm, Film movie)
	{
		int id_film;
		String naziv;
		double ocjena;
		Date datum_dodavanja;
		boolean aktivan;
		String sadrzaj;
		String slika_adresa;
		Time trajanje;
		String link_amazon;
		String link_imdb;
		
		ArrayList<Film> rezultat= new ArrayList<Film>();
		final String upit="select * from tbl_film where ocjena > "+Math.floor(movie.getOcjena())+" and ocjena < "+Math.ceil(movie.getOcjena());
		
		Connection conn = dm.checkOutConnection();

		if(conn!= null)
		{
			try
			{
				PreparedStatement ps= conn.prepareStatement(upit);
				try
				{
					ResultSet rs=ps.executeQuery();
					try
					{
						while(rs.next())
						{
							id_film=rs.getInt(1);
							naziv=rs.getString(2);
							ocjena=rs.getDouble(3);
							datum_dodavanja=rs.getDate(4);
							aktivan=rs.getBoolean(5);
							sadrzaj=rs.getString(6);
							slika_adresa=rs.getString(7);
							trajanje=rs.getTime(7);
							link_amazon=rs.getString(8);
							link_imdb=rs.getString(9);
							rezultat.add(new Film(id_film,naziv,ocjena,datum_dodavanja,aktivan,sadrzaj,slika_adresa,trajanje,link_amazon,link_imdb));
						}
					}
					finally
					{
						rs.close();
					}
				}
				finally
				{
					ps.close();
				}
			}
			catch(SQLException ex)
			{
				System.out.println("Greska u FilmDAO: "+ex.getMessage());
			}
			finally
			{
				dm.checkInConnection(conn);
			}
		}
		return rezultat;
	}
	
	public static ArrayList<Film> filterFilmByOcjenaAdmin(DataManager dm, double ocjena1)
	{
		int id_film;
		String naziv;
		double ocjena;
		Date datum_dodavanja;
		boolean aktivan;
		String sadrzaj;
		String slika_adresa;
		Time trajanje;
		String link_amazon;
		String link_imdb;
		
		ArrayList<Film> rezultat= new ArrayList<Film>();
		final String upit="select * from tbl_film where ocjena > "+Math.floor(ocjena1)+" and ocjena < "+Math.ceil(ocjena1);
		
		Connection conn = dm.checkOutConnection();

		if(conn!= null)
		{
			try
			{
				PreparedStatement ps= conn.prepareStatement(upit);
				try
				{
					ResultSet rs=ps.executeQuery();
					try
					{
						while(rs.next())
						{
							if(rs.getBoolean(5))
								continue;
							id_film=rs.getInt(1);
							naziv=rs.getString(2);
							ocjena=rs.getDouble(3);
							datum_dodavanja=rs.getDate(4);
							aktivan=rs.getBoolean(5);
							sadrzaj=rs.getString(6);
							slika_adresa=rs.getString(7);
							trajanje=rs.getTime(7);
							link_amazon=rs.getString(8);
							link_imdb=rs.getString(9);
							rezultat.add(new Film(id_film,naziv,ocjena,datum_dodavanja,aktivan,sadrzaj,slika_adresa,trajanje,link_amazon,link_imdb));
						}
					}
					finally
					{
						rs.close();
					}
				}
				finally
				{
					ps.close();
				}
			}
			catch(SQLException ex)
			{
				System.out.println("Greska u FilmDAO: "+ex.getMessage());
			}
			finally
			{
				dm.checkInConnection(conn);
			}
		}
		return rezultat;
	}

	public static boolean addFilm(DataManager dm, Film film)
	{
		boolean result=false;
		final String upit= "insert into tbl_film values(null,?,?,curdate(),?,?,?,?,?,?)";
		
		Connection conn=dm.checkOutConnection();
		
		if(conn!=null)
		{
			try
			{
				PreparedStatement ps= conn.prepareStatement(upit);
				ps.setString(1, film.getNaziv());
				ps.setDouble(2, film.getOcjena());
				//ps.setDate(3, (java.sql.Date) film.getDatum_dodavanja());
				ps.setBoolean(3,film.isAktivan());
				ps.setString(4, film.getSadrzaj());
				ps.setString(5, film.getSlika_adresa());
				ps.setTime(6, film.getTrajanje());
				ps.setString(7, film.getLink_amazon());
				ps.setString(8, film.getLink_imdb());
				result=ps.execute();
				ps.close();
			}
			catch(SQLException ex)
			{
				System.out.println("Greska u FilmDAO: "+ex.getMessage());
			}
			finally
			{
				dm.checkInConnection(conn);
			}
		}
		return result;
	}
	public static boolean updateFilm(DataManager dm, Film film)
	{
		boolean result=false;
        final String upit= "update tbl_film set naziv=?, ocjena=?, aktivan=?, sadrzaj=?, slika_adresa=?, trajanje=?, link_amazon=?, link_imdb=? where id_film = "+film.getId_film();
		
		Connection conn=dm.checkOutConnection();
		
		if(conn!=null)
		{
			try
			{
				PreparedStatement ps= conn.prepareStatement(upit);
				ps.setString(1, film.getNaziv());
				ps.setDouble(2, film.getOcjena());
				//ps.setDate(3, (java.sql.Date) film.getDatum_dodavanja());
				ps.setBoolean(3,film.isAktivan());
				ps.setString(4, film.getSadrzaj());
				ps.setString(5, film.getSlika_adresa());
				ps.setTime(6, film.getTrajanje());
				ps.setString(7, film.getLink_amazon());
				ps.setString(8, film.getLink_imdb());
				result=ps.execute();
				ps.close();
			}
			catch(SQLException ex)
			{
				System.out.println("Greska u FilmDAO: "+ex.getMessage());
			}
			finally
			{
				dm.checkInConnection(conn);
			}
		}
		return result;
	}
	
	public static boolean deleteFilm(DataManager dm, Film film)
	{
		boolean result=false;
         final String upit= "update tbl_film set aktivan=? where id_film = "+film.getId_film();
		
		Connection conn=dm.checkOutConnection();
		
		if(conn!=null)
		{
			try
			{
				PreparedStatement ps= conn.prepareStatement(upit);
				
				ps.setBoolean(1,false);
				
				result=ps.execute();
				ps.close();
			}
			catch(SQLException ex)
			{
				System.out.println("Greska u FilmDAO: "+ex.getMessage());
			}
			finally
			{
				dm.checkInConnection(conn);
			}
		}
		return result;
	}
	public static boolean deleteFilmById(DataManager dm, int id)
	{
		boolean result=false;
        final String upit= "update tbl_film set aktivan=? where id_film = "+id;
		
		Connection conn=dm.checkOutConnection();
		
		if(conn!=null)
		{
			try
			{
				PreparedStatement ps= conn.prepareStatement(upit);
				
				ps.setBoolean(1,false);
				
				result=ps.execute();
				ps.close();
			}
			catch(SQLException ex)
			{
				System.out.println("Greska u FilmDAO: "+ex.getMessage());
			}
			finally
			{
				dm.checkInConnection(conn);
			}
		}
		return result;
	}
}
